package com.yst.suivi.data.rights;

import java.rmi.RemoteException;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.StringTokenizer;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.EntityBean;

import com.yst.suivi.data.AbstractEntityBean;

/**
 * @author stolf
 * @ejb.bean name="Profil" type="CMP" cmp-version="2.x" schema="profil"
 *           local-jndi-name="ProfilBean" view-type="local" primkey-field = "id"
 *           reentrant = "True"
 * @ejb.home generate="local"
 * @ejb.persistence table-name = "profil"
 * @jboss.entity-command name = "key-generator"
 * @jboss.read-ahead eager-load-group = "most" strategy = "on-load"
 *                   page-size="25"
 * @jboss.load-group name="most"
 * @ejb.interface local-extends = "com.yst.suivi.data.util.NamedObject,
 *                javax.ejb.EJBLocalObject"
 * @ejb.finder signature="java.util.Collection findAll()" query="SELECT
 *             OBJECT(g) FROM profil AS g"
 * @ejb.finder signature="java.util.Collection
 *             findForUser(com.yst.suivi.data.user.User usr)" query="SELECT
 *             OBJECT(g) FROM profil AS g, IN (g.users) AS equipe WHERE equipe=?1"
 * 
 */
public abstract class ProfilBean extends AbstractEntityBean implements EntityBean
{
	private static final String RIGTHS_SEPARATOR = "#";

	private Hashtable mListRigths;

	/**
	 * @return
	 * @throws CreateException
	 * @ejb.create-method view-type = "local"
	 */
	public String ejbCreate() throws CreateException
	{
		return null;
	}

	public void ejbPostCreate()
	{
		// by default nothing to do
	}

	/**
	 * Return the primary key of the object
	 * 
	 * @ejb.interface-method view-type = "local"
	 * @ejb.persistence
	 * @jboss.jdbc-type type = "VARCHAR"
	 * @jboss.sql-type type = "VARCHAR(255)"
	 */
	public abstract String getId();

	/**
	 * Fix the primary key of the object. The method should be used only by the
	 * container
	 * 
	 * @ejb.interface-method view-type = "local"
	 */
	public abstract void setId(String id);

	/**
	 * Return the name of the object
	 * 
	 * @ejb.interface-method view-type = "local"
	 * @ejb.persistence
	 * @jboss.load-group name="most"
	 * @jboss.jdbc-type type = "VARCHAR"
	 * @jboss.sql-type type = "VARCHAR(255)"
	 */
	public abstract String getNom();

	/**
	 * Fix the name of the user. The method should be used only by the container
	 * 
	 * @ejb.interface-method view-type = "local"
	 */
	public abstract void setNom(String nom);

	/**
	 * Return the rights of the profil
	 * 
	 * @ejb.interface-method view-type = "local"
	 * @ejb.persistence
	 * @jboss.load-group name="most"
	 * @jboss.jdbc-type type = "VARCHAR"
	 * @jboss.sql-type type = "VARCHAR(2000)"
	 */
	public abstract String getRights();

	/**
	 * Fix the rights of the profil. The method should be used only by the
	 * container
	 * 
	 * @ejb.interface-method view-type = "local"
	 */
	public abstract void setRights(String rights);

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yst.suivi.data.AbstractEntityBean#ejbLoad()
	 */
	public void ejbLoad() throws EJBException, RemoteException
	{
		mListRigths = new Hashtable();
		if (null != getRights())
		{
			StringTokenizer st = new StringTokenizer(getRights(), RIGTHS_SEPARATOR);
			Right r;
			while (st.hasMoreTokens())
			{
				r = Right.get(Integer.parseInt(st.nextToken()));
				mListRigths.put(r.getCode(), r);
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yst.suivi.data.AbstractEntityBean#ejbPassivate()
	 */
	public void ejbPassivate() throws EJBException, RemoteException
	{
		saveRight();
	}

	/**
	 * 
	 */
	private void saveRight()
	{
		StringBuffer buf = new StringBuffer();
		Enumeration enumeration = mListRigths.keys();
		Object key;
		while (enumeration.hasMoreElements())
		{
			key = enumeration.nextElement();
			buf.append(((Right) mListRigths.get(key)).getCode().intValue());
			if (enumeration.hasMoreElements())
			{
				buf.append(RIGTHS_SEPARATOR);
			}
		}
		setRights(buf.toString());
	}

	/**
	 * Ajoute un droit au profil.
	 * 
	 * @param r
	 * @ejb.interface-method view-type = "local"
	 */
	public void add(Right r)
	{
		mListRigths.put(r.getCode(), r);
	}

	/**
	 * Ajoute un droit au profil.
	 * 
	 * @param r
	 * @ejb.interface-method view-type = "local"
	 */
	public void flushRight()
	{
		saveRight();
	}

	/**
	 * Supprime tous les droits.
	 * 
	 * @param r
	 * @ejb.interface-method view-type = "local"
	 */
	public void clearRight()
	{
		mListRigths = new Hashtable();
	}

	/**
	 * Supprime un droit au profil.
	 * 
	 * @param r
	 * @ejb.interface-method view-type = "local"
	 */
	public void remove(Right r)
	{
		mListRigths.remove(r.getCode());
	}

	/**
	 * 
	 * @ejb.interface-method view-type = "local"
	 */
	public Enumeration getRight()
	{
		return mListRigths.elements();
	}

	/**
	 * Retourne la liste des utilisateurs affectes au profil
	 * 
	 * @return Collection
	 * @ejb.interface-method view-type = "local"
	 * @ejb.relation name="ProfilUser" role-name = "ProfilUserRole"
	 *               target-multiple = "yes" target-role-name =
	 *               "ProfilUserTargerRole" target-ejb = "User"
	 * @jboss.relation-mapping style = "relation-table"
	 * @jboss.relation-table table-name = "LnkProgilUser"
	 * @jboss.relation fk-column = "idProfil" related-pk-field = "id"
	 * @jboss.target-relation fk-column = "idUser" related-pk-field = "id"
	 * @return Collection
	 */
	public abstract Collection getUsers();

	/**
	 * Positionne la liste de tous les utilisateurs qui apaprtiennent au profil.
	 * La collection doit contenir des objets User.
	 * 
	 * @param plf
	 * @ejb.interface-method view-type = "local"
	 */
	public abstract void setUsers(Collection users);
}
